//
//  Second.m
//  HYHOME
//
//  Created by cjy on 16/3/6.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Second.h"
#import "SpeakCell.h"
#import "SpeakModel.h"
#import "NetWorkHandler.h"
#import "PushSecond.h"
#import "DKNightVersion.h"
#import "SpeakTwoCell.h"
@interface Second ()<UITableViewDataSource,UITableViewDelegate,NetWorkHandlerDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)SpeakModel *model;
@end

@implementation Second



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem =[[ UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.title = @"业主说";
    [self createTableView];
    [self handle];
    
    
}


- (void)handle{
    NSString *str = @"http://api.wsq.umeng.com/v2/feeds/stream?os=iOS&openudid=c4fef44ea2c33987b846791b59bd050a622bea9b&sdkv=2.2.1&ak=5636cff167e58ef91b0001ae&de=iPhone5,2&mac=02:00:00:00:00:00&anonymous=1&count=40";
    
    [NetWorkHandler handlerJSONWithURL:str delegate:self];
    
}

- (void)haneldDidComplete:(id)result{
    
    self.array = [NSMutableArray array];
    NSArray *arr = [result objectForKey:@"items"];
    
    for (NSDictionary *dic in arr) {
        
        
        
        NSDictionary *dic2 = [dic objectForKey:@"creator"];
        SpeakModel *model = [[SpeakModel alloc]init];
        
        model.name = [dic2 objectForKey:@"name"];
        model.idNumber = [dic objectForKey:@"id"];
        NSDictionary *dic3 = [dic2 objectForKey:@"icon_url"];
        
        model.icon_url = [dic3 objectForKey:@"240"];
        
        
        model.content = [dic objectForKey:@"content"];
        
        model.create_time = [dic objectForKey:@"create_time"];
        
        
        NSArray *array2 = [dic objectForKey:@"image_urls"];
        if (array2.count == 0) {
            
            model.pic = nil;
            
        } else {
            
            NSDictionary *dicI = array2[0];
            model.pic = [dicI objectForKey:@"360"];
        }
        
        
        
        
        [self.array addObject:model];
        [model release];
        
        
    }
    
    
    [self.tableView reloadData];
    
}
- (void)createTableView{
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    [self.tableView registerClass:[SpeakCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[SpeakTwoCell class] forCellReuseIdentifier:@"pool2"];
    
    [self.tableView release];
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SpeakModel *model = [self.array objectAtIndex:indexPath.row];
    
    //自适应高度
    if (model.pic == nil){
        
        return [SpeakTwoCell heightForCell:model];
        
        
        
    }else{
        return 400;
    }
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.array.count ;
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SpeakModel *model =  [self.array objectAtIndex:indexPath.row];
    if (model.pic == nil) {
        
        
        SpeakTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        SpeakModel *model = [self.array objectAtIndex:indexPath.row ];
        
        
        cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor lightGrayColor]);
        
        
        
        
        [cell passModel:model];
        
        return cell;
        
        
    }else{
        SpeakCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        
        SpeakModel *model = [self.array objectAtIndex:indexPath.row ];
        
        
        cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor lightGrayColor]);
        
        
        
        [cell passModel:model];
        
        
        return cell;
        
        
    }
    
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SpeakModel *model = [[SpeakModel alloc]init];
    model = self.array[indexPath.row];
    
    if (model.pic != nil) {
        
        
        PushSecond *second = [[PushSecond alloc]init];
        second.idNumber =model.idNumber;
        
        
        [self.navigationController pushViewController:second animated:YES];
        
        [second release];
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
